package com.phapli.versionchecking.config;

import android.content.Context;
import android.content.pm.PackageManager;
import android.util.Log;

import com.phapli.versionchecking.http.GooglePlayParser;
import com.phapli.versionchecking.http.UpToDownParser;
import com.phapli.versionchecking.object.Item;

/**
 * Created by phapli on 2/9/2015.
 */
public class Config {


    public static final String intent_name = "name";

    //public static final String url = "http://tomicalab.com:1290/JsonTest/JsonResponse";
    public static final String url = "http://192.168.0.103:8080/JsonTest/JsonResponse";

    public static final Long TIME_IN_ONE_DAY = Long.valueOf(24 * 60 * 60 * 1000);

    //public static final long duration = AlarmManager.INTERVAL_DAY;
    public static final long duration = 60 * 2 * 1000;

    public static final int hour = 8;

    public static final int minute = 30;

    public static final String LOG = "VERSION CHECKING";
    public static final String version = "Version";

    public static boolean appInstalledOrNot(Context context, String uri) {
        PackageManager pm = context.getPackageManager();
        boolean app_installed = false;
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            app_installed = true;
        } catch (PackageManager.NameNotFoundException e) {
            app_installed = false;
        }
        return app_installed;
    }

    public static String getVersionName(Context context, String appPackage) {
        if (appPackage == null || appPackage.equals("")) {
            appPackage = context.getPackageName();
        }
        try {
            return context.getPackageManager()
                    .getPackageInfo(appPackage, 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(LOG, e.getMessage());
        }
        return "";
    }

    public static int getVersionCode(Context context, String appPackage) {
        if (appPackage == null || appPackage.equals("")) {
            appPackage = context.getPackageName();
        }
        try {
            return context.getPackageManager()
                    .getPackageInfo(appPackage, 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(LOG, e.getMessage());
        }
        return -1;
    }

    public static Item[] initItems() {
        Item[] items= new Item[3];
        items[0] = (new Item(0, "", "Installed", "", null));
        items[1] = (new Item(1, "https://play.google.com/store/apps/details?id=com.dotgears.swing", "Google Play Store", "", new GooglePlayParser()));
        items[2] = (new Item(2, "http://swing-copters.en.uptodown.com/android", "UpToDown Site", "", new UpToDownParser()));
        return items;
    }
}
